package com.syh.jn.scm.service.impl.upstream.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.upstream.entity.system.UpAccount;
import com.syh.jn.scm.domain.upstream.entity.system.UpRole;
import com.syh.jn.scm.domain.upstream.repository.system.UpAccountRepository;
import com.syh.jn.scm.domain.upstream.repository.system.UpRoleRepository;
import com.syh.jn.scm.dto.operation.system.permission.PermissionVo;
import com.syh.jn.scm.dto.upstream.system.UpMenuVo;
import com.syh.jn.scm.dto.upstream.system.upRole.UpRoleMto;
import com.syh.jn.scm.dto.upstream.system.upRole.UpRoleSto;
import com.syh.jn.scm.dto.upstream.system.upRole.UpRoleVo;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.upstream.system.UpRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class UpRoleServiceImpl extends AbstractServiceImpl<UpRoleSto, UpRoleMto, UpRoleVo, UpRole, Long> implements UpRoleService {

  @Autowired
  private UpRoleRepository upRoleRepository;
  @Override
  protected AggregateRepository<UpRole, Long> getAggregateRepository() {
    return this.upRoleRepository;
  }

  /**
   * 更新角色菜单
   *
   * @param roleId
   * @param menuIds
   */
  @Override
  public void updateRoleMenus(Long roleId, List<Long> menuIds) {
    UpRole role = upRoleRepository.getById(roleId);
    ExtAssert.notNull(role, "对象不存在");
    role.updateMenus(menuIds);
    upRoleRepository.update(role, true);
  }

  @Override
  public Long save(UpRoleSto sto) {
    UpRole upRole = new UpRole(sto);
    return upRoleRepository.add(upRole);
  }

  @Override
  public void update(UpRoleMto upRoleMto) {
    ExtAssert.notNull(upRoleMto, "对象为空");
    ExtAssert.notNull(upRoleMto.getId(), "id为空");
    UpRole upRole = upRoleRepository.getById(upRoleMto.getId());
    upRole.modify(upRoleMto);
    upRoleRepository.update(upRole);
  }

  @Override
  public void delete(Long id) {
    UpRole upRole = upRoleRepository.getById(id);
    if (upRole == null) {
      return;
    }
    upRole.destroy();
    upRoleRepository.removeById(id);
  }

	@Override
	public List<PermissionVo> getPermissionByRoleId(Long roleId) {
		return upRoleRepository.getPermissionByRoleId(roleId);
	}

  @Override
  public UpRoleVo getRole(Long accountId) {
    ExtAssert.notNull(accountId, "id为空");
    UpAccountRepository upAccountRepository = (UpAccountRepository) SpringContextUtils.getBean(UpAccountRepository.class);
    UpAccount upAccount = upAccountRepository.getById(accountId);
    if (upAccount == null) {
      throw new BusinessException("该账号无数据存在");
    }
    UpRole role = upRoleRepository.getById(upAccount.getRoleId());
    if (role == null) {
      return null;
    }
    UpRoleVo roleVo = new UpRoleVo();
    ExtBeanUtils.copyProperties(role, roleVo);
    return roleVo;
  }

  /**
   * 获取角色菜单
   *
   * @param wrapper
   */
  @Override
  public List<UpMenuVo> getMenus(Wrapper wrapper) {
    return this.upRoleRepository.getMenus(wrapper);
  }

  @Override
  public List<UpRoleVo> getAllRolesWithPermission() {
    List<UpRoleVo> roles = upRoleRepository.getAllRolesWithPermission();
    return roles;
  }

  @Override
  public List<UpRoleVo> getRolesByPermissionId(Long perId) {
    return upRoleRepository.getRolesByPermissionId(perId).stream().map(role -> {
      UpRoleVo roleVo = new UpRoleVo();
      ExtBeanUtils.copyProperties(role, roleVo);
      return roleVo;
    }).collect(Collectors.toList());
  }
}
